package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Fruit;
import com.itheima.edu.info.manager.service.FruitService1;

import java.util.Scanner;

public class CustomerController {
    private FruitService1 fruitService = new FruitService1();
    private Scanner sc = new Scanner(System.in);
    private static int sum = 0;

    public final void start() {
        Loop:
        while (true) {
            System.out.println("请输入您的选择: 1.查看水果  2.购买水果  3.结账  4.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    findAllFruit();
                    break;
                case "2":
                    buyFruit();
                    break;
                case "3":
                    billFruit();
                    break;
                case "4":
                    System.out.println("感谢您的惠顾!欢迎下次光临!");
                    break Loop;
                default:
                    System.out.println("您的输入有误, 请重新输入!");
                    break;
            }
        }
    }

    // 结账方法
    private void billFruit() {
        if (sum == 0) {
            System.out.println("您还没有购买水果,请先选择购买!");
            return;
        }
        if (sum > 0) {
            System.out.println("您购买的水果总金额为" + sum + "元，您需要支付" + sum + "元。");
        }
        sum = 0;
    }

    // 购买方法
    public void buyFruit() {
        Fruit[] fruits = fruitService.findAllFruit();
        // for循环,遍历水果数组
        for (int i = 0; i < fruits.length; i++) {
            Fruit f = fruits[i];
            if (f != null) {
                String number = null;
                while (true) {
                    System.out.println("您要购买 " + f.getFruitName() + " 多少斤?");
                    number = sc.next();
                    if (Integer.parseInt(number)>=0){
                        break;
                    }else{
                        System.out.println("输入非法,请输入正确的数字");
                    }
                }
                int fruitNumber = 0;
                try {
                    fruitNumber = Integer.parseInt(number);
                        int newInventory = f.getInventory() - fruitNumber;
                        int oldInventory = f.getInventory();
                        f.setInventory(newInventory);
                        if (newInventory < 0) {
                            System.out.println(f.getFruitName() + "剩余"+oldInventory+"斤，您可以买"+oldInventory+"斤"+f.getFruitName());
                            f.setInventory(0);
                            fruitNumber = oldInventory;
                        }

                } catch (Exception e) {
                    i--;
                    continue;
                }
                if (i == fruits.length - 1) {
                    System.out.println("您已购买结束,请去结账!");
                }
                sum += f.getFruitPrice() * fruitNumber;
            }
        }
    }

    // 查看方法
    private void findAllFruit() {
        Fruit[] fruits = fruitService.findAllFruit();
        if (fruits == null) {
            System.out.println("暂无信息, 请等待管理员添加后再查看!");
            return;
        }

        System.out.println("编号\t名称\t价格(元/斤)\t\t库存(斤)");
        for (int i = 0; i < fruits.length; i++) {
            Fruit f = fruits[i];
            if (f != null) {
                System.out.println(f.getId() + "\t\t" + f.getFruitName() + "\t" + f.getFruitPrice() + "\t\t\t\t" + f.getInventory());
            }
        }
    }
}
